<?php
/**
 * User: wufeng
 * Date: 2019/4/18
 * Time: 15:02.
 */

namespace app\admin\service;

use app\api\controller\Refund;
use app\models\CampusModel;
use app\models\ContractModel;
use app\models\ReceivablesModel;
use app\models\RefundAmountModel;

class RefundService
{
    /**
	 * 随行付退款
     * User: wufeng
     * Date: 2019/4/18.
     *
     * @param $param
     *
     * @throws \think\Exception
     * @throws \think\exception\DbException
     *
     * @return array
     */
    public function refund($param)
    {
        $contract = ContractModel::get(['contract_number' => $param['contract_number']]);
        $receivables = ReceivablesModel::get(['receipt_number' => $param['receipt_number']]);
        $refundData['contract_number'] = $contract['contract_number'];
        $refundData['receivables_id'] = $receivables['id'];
        $refundData['student_name'] = $contract['student_name'];
        $refundData['student_name'] = $contract['student_name'];
        $refundData['experienced_person'] = get_current_id();
        $refundData['refund_channel'] = $receivables['payment_method'];
        $refundData['receipt_number'] = $param['refund_voucher'];
        $refundData['refund_status'] = 0;
        $refundData['amount'] = $param['amount'] * 100;
        $refundData['total_amount'] = $contract['total_amount'];
        $refundData['balance'] = $contract['balance'] - $refundData['amount'];
        $refundData['refund_amount'] = $param['amount'] * 100;
        $refundData['campus_id'] = $contract['campus_id'];  //校区ID
        $refundData['campus_name'] = CampusModel::get(['id' => $contract['campus_id']])['campus_name']; //校区名字
        RefundAmountModel::create($refundData);
        return (new Refund())->refund($param['refund_voucher'], $param['receipt_number']);
    }
}
